/**************************************************************************************
 *
 * @(#) UserDetailsServiceImpl.java
 * @Package com.jgdt.jgdtkasannong_university_gene_backend.service.impl
 **************************************************************************************/
package com.jgdt.jgdtkasannong_university_gene_backend.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jgdt.jgdtkasannong_university_gene_backend.entity.user.SysUser;
import com.jgdt.jgdtkasannong_university_gene_backend.mapper.user.UserMapper;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
/**
 * @description:
 * @author: huangAn
 * @createDate: 2025/04/18
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private final UserMapper userMapper;

    public UserDetailsServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userMapper.selectOne(new QueryWrapper<SysUser>().eq("username", username));
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return User.builder()
                .username(user.getUsername())
                .password(user.getPassword())
                .disabled(user.getEnabled() != 1)
                .roles("USER")
                .build();
    }
}



